﻿/*
Copyright (c) 2008 William Duff

Permission is hereby granted, free of charge, to any person obtaining a copy 
of this software and associated documentation files (the "Software"), to deal 
in the Software without restriction, including without limitation the rights 
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 
copies of the Software, and to permit persons to whom the Software is furnished
to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all 
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 
SOFTWARE.
*/

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using LiveUpload.Facebook.Properties;

namespace LiveUpload.Facebook
{
    public partial class FacebookAccountLinkButton : RadioButton
    {
        #region Constructors

        /// <summary>
        /// Constructs a new FacebookAccountLink to represent an link between a
        /// Facebook friend and a person tag.
        /// </summary>
        public FacebookAccountLinkButton()
        {
            InitializeComponent();

            // Radio buttons don't handle double clicks by default, so we set the style manually.
            SetStyle(ControlStyles.StandardClick, true);
            SetStyle(ControlStyles.StandardDoubleClick, true);

            // These tooltips are used to give more information about linking accounts.
            this.facebookFriendConfirmedToolTip.SetToolTip(this.facebookFriendConfirmed, 
                Resources.FacebookFriendConfirmedToolTipText);
            this.facebookFriendConfirmedToolTip.SetToolTip(this.facebookFriendConfirmedImage, 
                Resources.FacebookFriendConfirmedToolTipText);
            this.facebookFriendNotConfirmedToolTip.SetToolTip(this.facebookFriendNotConfirmed, 
                Resources.FacebookFriendNotConfirmedToolTipText);
            this.facebookFriendNotConfirmedToolTip.SetToolTip(this.facebookFriendNotConfirmedImage, 
                Resources.FacebookFriendNotConfirmedToolTipText);
        }

        #endregion

        #region Private Fields

        private AccountLink accountLink;
        private List<Friend> facebookFriends;

        #endregion

        #region Public Fields

        /// <summary>
        /// The user that this radio button represents.
        /// </summary>
        public AccountLink AccountLink
        {
            get { return this.accountLink; }
            set
            {
                if (this.accountLink != value)
                {
                    this.accountLink = value;
                    OnAccountLinkChanged(new EventArgs());
                }
            }
        }

        /// <summary>
        /// The list of Facebook friends to choose from for the link.
        /// </summary>
        public List<Friend> FacebookFriends
        {
            get { return this.facebookFriends; }
            set
            {
                if (this.facebookFriends != value)
                {
                    this.facebookFriends = value;
                    OnFacebookFriendsChanged(new EventArgs());
                }
            }
        }

        /// <summary>
        /// The Facebook friend's picture.
        /// </summary>
        public PictureBox PersonTagSquarePicture
        {
            get { return this.personTagSquarePictureBox; }
            set { this.personTagSquarePictureBox = value; }
        }

        /// <summary>
        /// The Facebook friend's name.
        /// </summary>
        public string FacebookFriendName
        {
            get { return this.facebookFriendsComboBox.Text; }
        }

        #endregion

        #region Private Fields

        /// <summary>
        /// This event is fired when the account link associated with this radio button is changed.
        /// </summary>
        private void OnAccountLinkChanged(EventArgs eventArgs)
        {
            if (this.accountLink != null)
            {
                // Set a tool tip on the name label in case it auto-ellipsises.
                this.personTagNameToolTip.Active = true;
                this.personTagNameToolTip.SetToolTip(this.personTagName, this.accountLink.PersonTag.Name);

                // Display the user name.
                this.personTagName.Text = this.accountLink.PersonTag.Name;
            }
        }

        /// <summary>
        /// This event is fired when the list of facebook friends is changed.
        /// </summary>
        private void OnFacebookFriendsChanged(EventArgs eventArgs)
        {
            this.facebookFriendsComboBox.Items.AddRange(facebookFriends.ToArray());
            if (this.accountLink.FacebookFriend == null)
            {
                this.facebookFriendsComboBox.Text = this.accountLink.PersonTag.Name;
            }
            else
            {
                this.facebookFriendsComboBox.Text = this.accountLink.FacebookFriend.Name;
            }
            this.facebookFriendsComboBox.AutoCompleteMode = AutoCompleteMode.Suggest;
            this.facebookFriendsComboBox.AutoCompleteSource = AutoCompleteSource.ListItems;
        }

        /// <summary>
        /// This event is fired when the text changes in the facebook friends combo box.
        /// </summary>
        private void facebookFriendComboBox_TextChanged(object sender, EventArgs e)
        {
            // If a combo box item has been selected, grab it.
            Friend facebookFriend = this.facebookFriendsComboBox.SelectedItem as Friend;

            // If the user typed the name of the facebook friend instead of selecting
            // it from the list, we need to manually do a search for the matching friend.
            if (facebookFriend == null && this.facebookFriendsComboBox.Text != "")
            {
                int friendIndex = this.facebookFriends.FindIndex(delegate(Friend friend)
                {
                    return this.facebookFriendsComboBox.Text == friend.Name;
                });

                if(friendIndex >= 0)
                {
                    facebookFriend = this.facebookFriendsComboBox.Items[friendIndex] as Friend;
                }
            }

            if (facebookFriend != null && facebookFriend.Name == this.facebookFriendsComboBox.Text)
            {
                // Save the facebook friend.
                this.accountLink.FacebookFriend = facebookFriend;

                // Asynchronously load the user's profile picture.
                if (facebookFriend.SquarePictureUrl.StartsWith("http"))
                {
                    this.facebookFriendSquarePictureBox.LoadAsync(facebookFriend.SquarePictureUrl);
                }
                else
                {
                    this.facebookFriendSquarePictureBox.Image = Properties.Resources.Silhouette;
                }

                // Give the user a confirmation that a Facebook friend was chosen.
                this.facebookFriendConfirmed.Show();
                this.facebookFriendConfirmedImage.Show();
                this.facebookFriendConfirmedToolTip.Active = true;
                this.facebookFriendNotConfirmed.Hide();
                this.facebookFriendNotConfirmedImage.Hide();
                this.facebookFriendNotConfirmedToolTip.Active = false;
            }
            else
            {
                this.accountLink.FacebookFriend = null;

                this.facebookFriendSquarePictureBox.CancelAsync();
                this.facebookFriendSquarePictureBox.Image = Properties.Resources.Silhouette;

                // Show the confirmation that a Facebook friend was NOT chosen.
                this.facebookFriendConfirmed.Hide();
                this.facebookFriendConfirmedImage.Hide();
                this.facebookFriendConfirmedToolTip.Active = false;
                this.facebookFriendNotConfirmed.Show();
                this.facebookFriendNotConfirmedImage.Show();
                this.facebookFriendNotConfirmedToolTip.Active = true;
            }

            // Keep the name of the friend and the text in the combo box synchronized.
            this.facebookFriendName.Text = this.facebookFriendsComboBox.Text;
        }

        /// <summary>
        /// This event is fired when a child control is clicked on.
        /// </summary>
        private void childControl_Click(object sender, EventArgs e)
        {
            OnClick(e);

            // Make sure this radio button gets selected as a result of the click.
            Select();
        }

        /// <summary>
        /// This event is fired when a mouse enters the area of a child control.
        /// </summary>
        private void childControl_MouseEnter(object sender, EventArgs e)
        {
            OnMouseEnter(e);
        }

        /// <summary>
        /// This event is fired when this radio button is selected or deselected.
        /// </summary>
        private void FacebookAccountLinkButton_CheckedChanged(object sender, EventArgs e)
        {
            if (this.Checked)
            {
                // Hide the current name of the album.
                this.facebookFriendName.Hide();

                // Show a text box to allow the user to change the name of the album.
                this.facebookFriendsComboBox.Show();
                this.facebookFriendsComboBox.Focus();
                this.facebookFriendsComboBox.Select(this.facebookFriendsComboBox.Text.Length, 0);
            }
            else if (this.facebookFriendsComboBox.Visible)
            {
                // Hide the combo box and go back to showing the current name of the Facebook friend.
                this.facebookFriendsComboBox.Hide();
                this.facebookFriendName.Show();
            }
        }

        #endregion
    }
}
